package TankFrst;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class TankClient extends Frame {
    public static final int GAME_HIGH = 600;//设置高度
    public static final int GAME_WEIGHT = 800;//设置宽度

    Tank myTank = new Tank(50, 50, true, Dir.STOP, this);

    //集合储存爆炸，子弹，敌人坦克
    List<Explode> explodes = new ArrayList<Explode>();
    List<Missile> missiles = new ArrayList<Missile>();
    List<Tank> tanks = new ArrayList<>();

    Image offScreenImage = null;

    NetClient nc = new NetClient(this);

    ConnDialog dialog=new ConnDialog();

    @Override
    public void paint(Graphics g) {
        g.drawString("missiles count:" + missiles.size(), 10, 50);
        g.drawString("explores count:" + explodes.size(), 10, 70);
        g.drawString("Tanks    count:" + tanks.size(), 10, 90);

        //遍历子弹
        for (int i = 0; i < missiles.size(); i++) {
            Missile m = missiles.get(i);
//            m.hitTanks(tanks);//打击敌方坦克
            if (m.hitTank(myTank)){   //打击自己的坦克
                TankDeadMsg msg = new TankDeadMsg(myTank.id);
                nc.send(msg);
                MissileDeadMsg msmMsg=new MissileDeadMsg(m.tankId,m.id);
                nc.send(msmMsg);
            }
            m.draw(g);//画出子弹
        }

        //遍历爆炸图片
        for (int i = 0; i < explodes.size(); i++) {
            Explode e = explodes.get(i);
            e.draw(g);
        }

        //遍历坦克
        for (int i = 0; i < tanks.size(); i++) {
            Tank t = tanks.get(i);
//            t.collidesWithTanks(tanks);
            t.draw(g);//画出敌方坦克
        }
        myTank.draw(g);//画出自己的坦克
    }

    //利用双缓冲解决画面卡顿问题
    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WEIGHT, GAME_HIGH);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.green);
        gOffScreen.fillRect(0, 0, GAME_WEIGHT, GAME_HIGH);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    public void lauchFrame() {
        this.setLocation(400, 300);//设置窗口的位置
        this.setSize(GAME_WEIGHT, GAME_HIGH);//设置窗口可见
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);//设置窗口退出
            }
        });
        this.setResizable(false);//设置窗口不能变换大小
        this.setBackground(Color.green);
        this.addKeyListener(new KeyMonitor());//设置监听器
        setVisible(true);
        new Thread(new PaintThread()).start();//设置线程启动
//        nc.connect("127.0.0.1", TankServer.TCP_PORT);
    }

    public static void main(String[] args) {
        TankClient tankClient = new TankClient();
        tankClient.lauchFrame();
    }

    //设置线程
    private class PaintThread implements Runnable {
        @Override
        public void run() {
            while (true) {
                repaint();//重绘
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //定义监听器
    private class KeyMonitor extends KeyAdapter {
        //键盘按下的监听器
        public void keyPressed(KeyEvent e) {
            int key=e.getKeyCode();
            if(key==KeyEvent.VK_C){
                dialog.setVisible(true);
            }else{
                myTank.keyPressed(e);
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            myTank.keyReleased(e);
        }
        //键盘松开的监听器
    }
    // STOPSHIP: 2022/4/20 12：00

    public static class TankServer {
        public static final int TCP_PORT = 8888;

        public static void main(String[] args) {
            try {
                ServerSocket serverSocket = new ServerSocket(TCP_PORT);
                while (true) {
                    Socket s = serverSocket.accept();
                    System.out.println("A Client Connect! Addr-" + s.getInetAddress() + ":" + s.getPort());

                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
    //让其自动生成端口号
    class ConnDialog extends Dialog{
//设置在方法体外可以让下面的监听器调用
        Button b=new Button("确定");
        TextField tfIP=new TextField("127.0.0.1",12);
        TextField tfPort=new TextField(""+TankServer.TCP_PORT,4);
        TextField tfMyUDPPort=new TextField("2223",4);
        public ConnDialog(){
            super(TankClient.this,true);
            this.setLayout(new FlowLayout());//设置布局管理器
            this.add(new Label("IP:"));
            this.add(tfIP);
            this.add(new Label("Port:"));
            this.add(tfPort);//服务端端口号
            this.add(new Label("My UDP Port:"));
            this.add(tfMyUDPPort);//自己的udp端口
            this.add(b);
            this.setLocation(300,300);
            this.pack();
            //设置按叉的时候不显示
            this.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    setVisible(false);
                }
            });
            //处理确定键
            b.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String IP= tfIP.getText().trim();
                    int port=Integer.parseInt(tfPort.getText().trim());
                    int myUDPPort=Integer.parseInt(tfMyUDPPort.getText().trim());
                    nc.setUdpPort(myUDPPort);
                    nc.connect(IP,port);
                    setVisible(false);

                }
            });
        }
    }
}

